<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 后台菜单 服务
 */
class Admin_menu_service extends MY_Service {

    const ID  = 'menu_id';
    const PID = 'parent_id';

    protected $menus = array();

    public function __construct() {
        parent::__construct();

        $this->load->model('admin_menu_model');
        $this->menus = $this->admin_menu_model->get_all_menus();

        log_message('debug', 'Admin_menu_service Service Class Initialized');
    }

    /**
     * 获取菜单
     *
     * @return array array($menus, $menus_tree)
     */
    public function get_menus() {
        $active_menu_ids = $this->get_active_menu_ids();
        $menus = array();
        foreach ($this->menus as $menu_id => $menu) {
            if ($menu['status'] == 0) continue;

            //TODO:权限判断哪些菜单可以显示

            //激活的菜单
            if (in_array($menu_id, $active_menu_ids)) $menu['_css_class'] = 'active';

            //菜单分组
            if ($menu[self::PID] == 0) {
                //顶级分组
                $menus['lv1'][$menu_id] = $menu;
            }
            if (isset($active_menu_ids[0]) && $menu[self::PID] == $active_menu_ids[0]) {
                //左栏菜单
                $menus['lv2'][$menu_id] = $menu;
                //分组
                if ($menu['group']) {
                    $menus['lv2_group'][$menu['group']][$menu_id] = $menu;
                } else {
                    $menus['lv2_group'][$menu_id] = $menu;
                }
            }
            if (isset($active_menu_ids[1]) && $menu[self::PID] == $active_menu_ids[1]) {
                //右栏菜单
                $menus['lv3'][$menu_id] = $menu;
            }
        }
        $menus['menu'] = $this->get_active_menu();
        $menus['crumbs'] = $this->get_active_menus();
        return $menus;
    }

    /**
     * 获取全部激活的菜单ids
     *
     * @return array
     */
    public function get_active_menu_ids() {
        return array_keys($this->get_active_menus());
    }

    /**
     * 获取全部激活的菜单
     *
     * @return array
     */
    public function get_active_menus() {
        static $active_menus = array();
        if ($active_menus) return $active_menus;

        if ($menu = $this->get_active_menu()) {
            $this->load->helper('tree');
            $active_menus = list_find_parents($this->menus, $menu[self::ID], self::ID, self::PID);
        }

        return $active_menus;
    }

    /**
     * 获取当前激活的菜单
     *
     * @return array
     */
    public function get_active_menu() {
        static $active_menu = array();
        if ($active_menu) return $active_menu;

        foreach ($this->menus as $menu) {
            if ($menu['name'] == CLASS_NAME) {
                $active_menu = $menu;
            }
            if ($menu['name'] == CLASS_NAME . '/' . METHOD_NAME) {
                $active_menu = $menu;
                break;
            }
        }

        return $active_menu;
    }
}

/* End of file Admin_menu_service.php */
/* Location: ./application/services/admin_menu/Admin_menu_service.php */
